﻿using AutoCommunication.Enums;
using AutoCommunication.Helper;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO.Ports;

namespace AutoCommunication.Base
{
    /// <summary>
    /// SerialPort基类
    /// </summary>

    public abstract class BaseSerialPortDevice<Tvariable> : BaseDevice<Tvariable> where Tvariable : BaseVariable
    {
        public BaseSerialPortDevice()
        {
        }

        #region 属性

        /// <summary>
        /// 端口名
        /// </summary>
        [JsonProperty("端口名"), DisplayName("端口名"), Category("设备信息")]
        public string PortName { get; set; } = "COM2";

        /// <summary>
        /// 波特率
        /// </summary>
        [JsonProperty("波特率"), DisplayName("波特率"), Category("设备信息")]
        public int BaudRate { get; set; } = 9600;

        /// <summary>
        /// 校验位
        /// </summary>
        [JsonProperty("校验位"), DisplayName("校验位"), Category("设备信息")]
        public Parity Parity { get; set; } = Parity.None;

        /// <summary>
        /// 数据位
        /// </summary>
        [JsonProperty("数据位"), DisplayName("数据位"), Category("设备信息")]
        public int DataBits { get; set; } = 8;

        /// <summary>
        /// 停止位
        /// </summary>
        [JsonProperty("停止位"), DisplayName("停止位"), Category("设备信息")]
        public StopBits StopBits { get; set; } = System.IO.Ports.StopBits.One;

        /// <summary>
        /// Encoding格式
        /// </summary>
        [JsonProperty("Encoding格式"), DisplayName("Encoding格式"), Category("设备信息")]
        public EncodingEnum EncodingType { get; set; } = EncodingEnum.ASCII;

        /// <summary>
        /// 是否为plc地址 默认false
        /// </summary>
        [JsonProperty("是否为plc地址"), DisplayName("是否为plc地址"), Category("设备信息")] public bool plcAddresses { get; set; } = false;

        #endregion 属性

        /// <summary>
        /// 串行端口对象
        /// </summary>
        [JsonIgnore] public SerialPort serialPort = new SerialPort();

        /// <summary>
        /// 获取设备上的COM端口集合
        /// </summary>
        /// <returns></returns>
        public static string[] GetPortNames()
        {
            return SerialPort.GetPortNames();
        }

        /// <summary>
        /// 连接:会先断开在连接//如果是特殊协议,可能需要重写此方法
        /// </summary>
        /// <returns></returns>
        [Obsolete]
        protected override Result Connect()
        {
            var result = new Result();
            if (serialPort == null) serialPort = new SerialPort();
            serialPort?.Close();
            serialPort.PortName = PortName;
            serialPort.BaudRate = BaudRate;
            serialPort.DataBits = DataBits;
            serialPort.StopBits = StopBits;
            serialPort.Encoding = Common.GetEncoding(EncodingType);
            serialPort.Parity = Parity;
            serialPort.ReadTimeout = Timeout;
            serialPort.WriteTimeout = Timeout;
            plcAddresses = plcAddresses;
            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                if (serialPort?.IsOpen ?? false) serialPort?.Close();
                result.IsSucceed = false;
                result.Err = ex.Message;
                result.ErrCode = 408;
                result.Exception = ex;
            }
            return result.EndTime();
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        [Obsolete]
        protected override Result Disconnect()
        {
            var result = new Result();
            try
            {
                serialPort.Close();
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result;
        }
    }
}